package info.muge.appshare.adapters

import android.app.Activity
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import com.gushenge.core.beans.IndexTop
import com.kyzh.core.utils.dip

class LanguageAdapter(context: Activity, val layout: Int, val beans: MutableList<IndexTop>):
    ArrayAdapter<IndexTop>(context, layout, beans){

    inner class ViewHolder(val textView: TextView)
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val viewHolder : ViewHolder
        val view :View
        if (convertView ==null){
            view = LayoutInflater.from(context).inflate(layout, null)
            val textView = view.findViewById<TextView>(android.R.id.text1)
            viewHolder = ViewHolder(textView)
            view.tag = viewHolder
        }else{
            view = convertView
            viewHolder = view.tag as ViewHolder
        }
        viewHolder.textView.text = beans[position].name
        viewHolder.textView.textAlignment = View.TEXT_ALIGNMENT_CENTER
        return view
    }

    override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
        val viewHolder : ViewHolder
        val view :View
        if (convertView ==null){
            view = LayoutInflater.from(context).inflate(layout, null)
            val textView = view.findViewById<TextView>(android.R.id.text1)
            viewHolder = ViewHolder(textView)
            view.tag = viewHolder
        }else{
            view = convertView
            viewHolder = view.tag as ViewHolder
        }
        getItem(position)?.let {
            viewHolder.textView.height = context.dip(40)
            viewHolder.textView.text = it.name
            viewHolder.textView.gravity = Gravity.CENTER
        }
        return view
    }
}
